package com.itheima.homework;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author 王加鑫
 * @Date 2021/5/22 21:26
 * @Version 1.0
 * 4、【编号2005】有一个存放牛奶的箱子，送奶工将牛奶放入箱子，而用户从箱子中获取到牛奶，
 * 假设箱子只能存储一瓶牛奶，请编写程序模拟牛奶的存储和获取过程。
 */
public class Demo05 {
    public static void main(String[] args) {
        Milk1 milk1 = new Milk1();
        Thread thread1 = new Thread(new Custom1(milk1));
        Thread thread2 = new Thread(new Person1(milk1));
        thread1.start();
        thread2.start();

    }

}

class Milk1{

    private  Lock lock = new ReentrantLock();

    //牛奶数量
    private int count;

    //牛奶是否喝完这个状态
    private boolean flag;

    //flag = true默认表示牛奶已经喝完了
    public Milk1() {
        this(10,true);
    }

    public Milk1(int count, boolean flag) {
        this.count = count;
        this.flag = flag;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public Lock getLock() {
        return lock;
    }

    public void setLock(Lock lock) {
        this.lock = lock;
    }
}


/**
 * 用户
 */
class Custom1 implements Runnable{

    private Milk1 milk;

    public Custom1(Milk1 milk) {
        this.milk = milk;
    }

    @Override
    public void run() {
        int count = milk.getCount();
        boolean flag = milk.isFlag();
        while (true){
            try {
                milk.getLock().lock();
                if(count == 0){
                    break;
                }
                //牛奶已经喝完了,唤醒送奶工送奶
                if(flag){
                    System.out.println("客户在喝牛奶,还剩" + count + "瓶牛奶");
                    flag = false;
                    milk.notifyAll();
                    count--;
                }else {
                    milk.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                milk.getLock().unlock();
            }
        }


    }
}


/**
 * 送奶工
 */
class Person1 implements Runnable{

    private Milk1 milk;

    public Person1(Milk1 milk) {
        this.milk = milk;
    }

    @Override
    public void run() {
        int count = milk.getCount();
        boolean flag = milk.isFlag();
        while (true){
            try {
                milk.getLock().lock();
                if(count == 0){
                    break;
                }
                //牛奶喝完,唤醒客户喝牛奶
                if(!flag){
                    System.out.println("送奶工在送牛奶,还剩" + count + "瓶牛奶");
                    flag = true;
                    milk.notifyAll();
                }else {
                    milk.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                milk.getLock().unlock();
            }
        }
    }
}
